#ifndef GAMEMODELTHREAD_H
#define GAMEMODELTHREAD_H

#include "GameModel.h"

#include <QThread>
#include <QDebug>

class GameModelThread: public QThread
{
    Q_OBJECT
public:
    explicit GameModelThread(QObject *parent = nullptr);
    virtual ~GameModelThread();


public:
    virtual int InitOnce()         //进行初始化(仅1次,由外部配置好后调用)
    {
        return m_pGameModel->InitOnce();
    }
    virtual int InitOnceInThread() //线程中初始化(1次)
    {
        return m_pGameModel->InitOnceInThread();
    }
    //每次游戏开始前初始化,sr为随机数种子
    virtual int GameInit(int sr = -1)
    {
        return m_pGameModel->GameInit(sr);
    }
    //游戏开始调用
    /*_GameStatus GameBegin()
    {

    }*/
    //游戏结束
    virtual int sl_OverGame(GameModel::_GameStatus status = GameModel::Game_Status_GameOver, int code = 0)
    {
        return m_pGameModel->sl_OverGame(status, code);
    }


    //强制结束游戏,尽量调用后加wait()
    Q_INVOKABLE virtual int TerminateGame(GameModel::_GameStatus status = GameModel::Game_Status_Exit, int code = 0)
    {
        //!!!注意:因为游戏退出,不一定会真正将线程结束(因为有些函数返回Game_Button_ERROR!!!)
        //先暂时用这个判断!!!
        bool bGameIsRunning = this->isRunning();
        //bool bGameIsRunning = (gameStatus() ==  Game_Status_Continue);

        if(!bGameIsRunning)
            return 0;


        setGameStatus(status, code);
        qDebug() << "[GameModel]TerminateGame:" << status << code;
        return 1;

        //return m_pGameModel->TerminateGame(status, nCode);
    }

protected:

    //设置游戏状态
    virtual GameModel::_GameStatus setGameStatus(GameModel::_GameStatus status, int code)
    {
        return m_pGameModel->setGameStatus(status, code);
    }

    //检测游戏是否结束,返回Game_Status_Continue或Game_Status_Gameover
    virtual GameModel::_GameStatus CheckGameOver()
    {
        return m_pGameModel->CheckGameOver();
    }




//框架属性:
public:
    GameModel::_GameStatus gameStatus() const
    {
        return m_pGameModel->gameStatus();
    }




public:
    GameModel *operator->()
    {
        return m_pGameModel;
    }

protected:
    virtual void run() Q_DECL_OVERRIDE;

public:
    GameModel *m_pGameModel;

};

#endif // GAMEMODELTHREAD_H
